package day01;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.*;
import java.util.UUID;
import java.util.concurrent.ExecutorService;

@Data
@AllArgsConstructor
//@NoArgsConstructor
@Accessors(chain=true)
@ToString
class Order implements Externalizable {
    private static final long serialVersionUID = 1L;
    private String id;
    private String productName;
    private int num;
    private double price;
    private double sumMoney;
    private String userName;
    private String mobile;
    private String addr;

    public Order() {
        id = UUID.randomUUID().toString().substring(0,8);
        this.sumMoney = this.getNum()*this.getPrice();
    }

    public double getSumMoney() {
        return this.getNum()*this.getPrice();
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        System.out.println("write");
        out.writeObject("订单编号: "+this.id);
        out.writeChar('\t');
        out.writeObject("商品名称: "+this.productName);
        out.writeInt(this.num);
        out.writeObject("用户名称: "+this.userName);
        out.writeDouble(this.price);
        out.writeObject("手机号码: "+this.mobile);
        out.writeDouble(this.getSumMoney());
        out.writeObject("送货地址:"+this.addr);
        System.out.println("write over");
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        System.out.println("read");
        this.id = (String)in.readObject();
        in.readChar();
        this.productName=(String)in.readObject();
        this.num=in.readInt();
        this.userName=(String)in.readObject();
        this.price=in.readDouble();
        this.mobile = (String)in.readObject();
        this.sumMoney =in.readDouble();
        this.addr = (String) in.readObject();
        System.out.println("read over");
    }
}

public class ObjectInputOutputStreamDemo02 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Order order = new Order();
        order.setAddr("南京");
        order.setNum(3);
        order.setPrice(19.9);
        order.setMobile("66666");
        order.setUserName("张三");
        order.setProductName("圆珠笔");
        ObjectOutputStream oout = new ObjectOutputStream(
                new FileOutputStream(new File("D:\\1\\order2.txt"))
        );
        oout.writeObject(order);
        oout.flush();
        oout.close();
        ObjectInputStream oin = new ObjectInputStream(new FileInputStream(new File("D:\\1\\order2.txt")));
        Order order2 = (Order)oin.readObject();
        System.out.println(order2);
        oin.close();
    }
}
